From ba1112606e36825d31a9775d8a8b5f213d1c56c1 Mon Sep 17 00:00:00 2001 From: Carlo Camilloni Date: Tue, 14 Apr 2026 16:35:54 +0200 Subject: [PATCH 01/18] cmdata: new parser (xtc/trr/gro/pdb) --- tests/run_cmdata.sh | 2 +- tools/cmdata/CMakeLists.txt | 47 +- tools/cmdata/README.md | 17 +- tools/cmdata/src/cmdata/cmdata.hpp | 52 +- tools/cmdata/src/cmdata/frame.hpp | 101 + tools/cmdata/src/cmdata/molfile_support.hpp | 62 + tools/cmdata/src/molfile/COPYRIGHT | 35 + tools/cmdata/src/molfile/Gromacs.h | 2015 +++++++++++++++++ tools/cmdata/src/molfile/LICENSE | 35 + tools/cmdata/src/molfile/Makefile | 20 + tools/cmdata/src/molfile/README | 17 + tools/cmdata/src/molfile/crdplugin.cpp | 259 +++ .../src/molfile/create_static_header.sh | 80 + tools/cmdata/src/molfile/endianswap.h | 113 + tools/cmdata/src/molfile/fastio.h | 666 ++++++ tools/cmdata/src/molfile/gromacsplugin.cpp | 780 +++++++ tools/cmdata/src/molfile/import.sh | 71 + tools/cmdata/src/molfile/largefiles.h | 74 + tools/cmdata/src/molfile/libmolfile_plugin.h | 31 + tools/cmdata/src/molfile/molfile_plugin.h | 1030 +++++++++ tools/cmdata/src/molfile/pdbplugin.cpp | 644 ++++++ tools/cmdata/src/molfile/periodic_table.h | 198 ++ tools/cmdata/src/molfile/readpdb.h | 441 ++++ tools/cmdata/src/molfile/vmdplugin.h | 236 ++ 24 files changed, 6981 insertions(+), 45 deletions(-) create mode 100644 tools/cmdata/src/cmdata/frame.hpp create mode 100644 tools/cmdata/src/cmdata/molfile_support.hpp create mode 100644 tools/cmdata/src/molfile/COPYRIGHT create mode 100644 tools/cmdata/src/molfile/Gromacs.h create mode 100644 tools/cmdata/src/molfile/LICENSE create mode 100644 tools/cmdata/src/molfile/Makefile create mode 100644 tools/cmdata/src/molfile/README create mode 100644 tools/cmdata/src/molfile/crdplugin.cpp create mode 100755 tools/cmdata/src/molfile/create_static_header.sh create mode 100644 tools/cmdata/src/molfile/endianswap.h create mode 100644 tools/cmdata/src/molfile/fastio.h create mode 100644 tools/cmdata/src/molfile/gromacsplugin.cpp create mode 100644 tools/cmdata/src/molfile/import.sh create mode 100644 tools/cmdata/src/molfile/largefiles.h create mode 100644 tools/cmdata/src/molfile/libmolfile_plugin.h create mode 100644 tools/cmdata/src/molfile/molfile_plugin.h create mode 100644 tools/cmdata/src/molfile/pdbplugin.cpp create mode 100644 tools/cmdata/src/molfile/periodic_table.h create mode 100644 tools/cmdata/src/molfile/readpdb.h create mode 100644 tools/cmdata/src/molfile/vmdplugin.h diff --git a/tests/run_cmdata.sh b/tests/run_cmdata.sh index 04dd488e..4949c7bf 100755 --- a/tests/run_cmdata.sh +++ b/tests/run_cmdata.sh @@ -18,7 +18,7 @@ tar -zxf test_outputs/cmdata/hh.tgz -C test_outputs/cmdata/ failures=0 for i in $(ls -1 test_inputs/cmdata/histo); do - if ! python3 "$COMPARE" \ + if ! python "$COMPARE" \ test_inputs/cmdata/histo/$i \ test_outputs/cmdata/histo/$i \ --rtol "$RTOL" --atol "$ATOL"; then diff --git a/tools/cmdata/CMakeLists.txt b/tools/cmdata/CMakeLists.txt index abba786c..f813d22d 100644 --- a/tools/cmdata/CMakeLists.txt +++ b/tools/cmdata/CMakeLists.txt @@ -59,6 +59,33 @@ endif() # include source and header files target_include_directories(${CMDATA} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) +# --------------------------------------------------------------------------- +# VMD molfile plugins (bundled in src/molfile, originally from PLUMED2). +# Both .cpp files define VMDPLUGIN_init/register; per-source COMPILE_DEFINITIONS +# rename those entry points to avoid link-time symbol collisions. +# MOLFILE_NATIVE_NM keeps GROMACS-format coordinates in nm (avoids nm→Å→nm +# round-trip precision loss). The PDB plugin is left at default Å output. +# --------------------------------------------------------------------------- +set(_MOLFILE_COMMON "__PLUMED_HAS_MOLFILE_PLUGINS") +set(_GMX_RENAMES + "VMDPLUGIN_init=molfile_gromacsplugin_init" + "VMDPLUGIN_register=molfile_gromacsplugin_register" + "VMDPLUGIN_fini=molfile_gromacsplugin_fini" + "MOLFILE_NATIVE_NM") +set(_PDB_RENAMES + "VMDPLUGIN_init=molfile_pdbplugin_init" + "VMDPLUGIN_register=molfile_pdbplugin_register" + "VMDPLUGIN_fini=molfile_pdbplugin_fini") + +set_source_files_properties(src/molfile/gromacsplugin.cpp PROPERTIES + COMPILE_DEFINITIONS "${_MOLFILE_COMMON};${_GMX_RENAMES}") +set_source_files_properties(src/molfile/pdbplugin.cpp PROPERTIES + COMPILE_DEFINITIONS "${_MOLFILE_COMMON};${_PDB_RENAMES}") + +target_sources(${CMDATA} PRIVATE + src/molfile/gromacsplugin.cpp + src/molfile/pdbplugin.cpp) + include(FetchContent) SET(FETCHCONTENT_QUIET OFF) SET(FETCHCONTENT_BASE_DIR ${CMAKE_CURRENT_BINARY_DIR}/cmdata_fetch) @@ -79,25 +106,8 @@ if(_popt_inc) set_target_properties(popt PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${_popt_inc}") endif() -message(STATUS "Fetching xdrfile") -FetchContent_Declare( - xdrfile - URL https://github.com/multi-ego/xdrfile/archive/refs/heads/chemfiles.zip - DOWNLOAD_EXTRACT_TIMESTAMP TRUE -) -# xdrfile's CMakeLists.txt calls project() without VERSION; set the policy -# default so the warning is suppressed in that subdirectory. -set(CMAKE_POLICY_DEFAULT_CMP0048 NEW) -FetchContent_MakeAvailable(xdrfile) -# Suppress all warnings from xdrfile (third-party, no control) -target_compile_options(xdrfile PRIVATE -w) -get_target_property(_xdr_inc xdrfile INTERFACE_INCLUDE_DIRECTORIES) -if(_xdr_inc) - set_target_properties(xdrfile PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${_xdr_inc}") -endif() - # link libraries -target_link_libraries(${CMDATA} PRIVATE Gromacs::libgromacs xdrfile popt $<$:OpenMP::OpenMP_CXX>) +target_link_libraries(${CMDATA} PRIVATE Gromacs::libgromacs popt $<$:OpenMP::OpenMP_CXX>) # Workaround for macOS 26+ SDK: libm.tbd was removed because math functions are # now fully part of libSystem. GROMACS (and other deps) may export an absolute @@ -117,7 +127,6 @@ if(APPLE) endif() endmacro() strip_libm(Gromacs::libgromacs) - strip_libm(xdrfile) strip_libm(popt) strip_libm(${CMDATA}) endif() diff --git a/tools/cmdata/README.md b/tools/cmdata/README.md index 10da2c16..ff10f9e3 100644 --- a/tools/cmdata/README.md +++ b/tools/cmdata/README.md @@ -57,11 +57,20 @@ make && ctest cmdata -f TRAJ -s TOP [OPTIONS] ``` +### Supported trajectory formats + +| Extension | Format | +|-----------|--------| +| `.xtc` | GROMACS compressed trajectory | +| `.trr` | GROMACS full-precision trajectory | +| `.gro` | GROMACS coordinate file (multi-model) | +| `.pdb` | PDB coordinate file (multi-model) | + ### Required options | Option | Description | |--------|-------------| -| `-f`, `--traj=FILE` | Input trajectory file (`.xtc`) | +| `-f`, `--traj=FILE` | Input trajectory file (`.xtc`, `.trr`, `.gro`, or `.pdb`) | | `-s`, `--top=FILE` | Input topology file (`.tpr`) | | `--mode=STRING` | Calculation mode (see below) | @@ -144,3 +153,9 @@ OMP_NUM_THREADS=8 cmdata -f traj.xtc -s topol.tpr \ --weights weights.dat \ -o weighted/ ``` + +Read a full-precision TRR trajectory: + +```bash +OMP_NUM_THREADS=8 cmdata -f traj.trr -s topol.tpr --mode intra -o output/ +``` diff --git a/tools/cmdata/src/cmdata/cmdata.hpp b/tools/cmdata/src/cmdata/cmdata.hpp index a503854c..55b416dd 100644 --- a/tools/cmdata/src/cmdata/cmdata.hpp +++ b/tools/cmdata/src/cmdata/cmdata.hpp @@ -16,7 +16,8 @@ #include "indexing.hpp" #include "density.hpp" #include "mindist.hpp" -#include "xtc_frame.hpp" +#include "frame.hpp" +#include "molfile_support.hpp" // standard library imports #include @@ -27,9 +28,6 @@ #include #include -// xdrfile includes -#include -#include namespace cmdata { @@ -71,8 +69,7 @@ class CMData PbcType pbcType_; // frame fields - cmdata::xtc::Frame *frame_; - XDRFILE *trj_; + cmdata::traj::Frame *frame_; // density fields float dx_; @@ -205,25 +202,34 @@ class CMData set_pbc(pbc_, pbcType_, boxtop_); } - int natom; - long unsigned int nframe; - int64_t *offsets; - - frame_ = (cmdata::xtc::Frame*)malloc(sizeof(cmdata::xtc::Frame)); std::cout << "Reading trajectory file " << traj_path << std::endl; - read_xtc_header(traj_path.c_str(), &natom, &nframe, &offsets); - *frame_ = cmdata::xtc::Frame(natom); - frame_->nframe = nframe; - frame_->offsets = offsets; - - trj_ = xdrfile_open(traj_path.c_str(), "r"); + { + const std::size_t dot = traj_path.rfind('.'); + if (dot == std::string::npos) + throw std::runtime_error("Cannot determine trajectory format: no extension in '" + traj_path + "'"); + const std::string ext = traj_path.substr(dot + 1); + molfile_plugin_t *plugin = cmdata::get_molfile_plugin(ext); + int natom = 0; + void *handle = plugin->open_file_read(traj_path.c_str(), ext.c_str(), &natom); + if (!handle) + throw std::runtime_error("Cannot open trajectory file: " + traj_path); + // GROMACS formats (xtc/trr/gro/g96) are compiled with MOLFILE_NATIVE_NM, + // so their coordinates are already in nm — no unit conversion needed. + // PDB coordinates are in Angstrom and need *0.1 to reach nm. + const bool native_nm = (ext == "xtc" || ext == "trr" || ext == "gro" || ext == "g96"); + const float coord_scale = native_nm ? 1.0f : 0.1f; + frame_ = (cmdata::traj::Frame*)malloc(sizeof(cmdata::traj::Frame)); + *frame_ = cmdata::traj::Frame(natom, plugin, handle, coord_scale); + } initAnalysis(); } ~CMData() { + if (frame_->mf_handle && frame_->mf_plugin) + frame_->mf_plugin->close_file_read(frame_->mf_handle); + free(frame_->mf_coords); free(frame_->x); - free(frame_->offsets); free(frame_); free(mtop_); if (xcm_ != nullptr) free(xcm_); @@ -395,12 +401,10 @@ class CMData auto do_run = [this](auto f_intra, auto f_same, auto f_cross) { int frnr = 0; - float progress = 0.f; - cmdata::io::print_progress_bar(progress); - while (frame_->read_next_frame(trj_, no_pbc_, pbcType_, pbc_) == exdrOK) + while (frame_->read_next_frame(no_pbc_, pbcType_, pbc_) == cmdata::traj::FRAME_OK) { - float new_progress = static_cast(frnr) / static_cast(frame_->nframe); - if (new_progress - progress > 0.01f) { progress = new_progress; cmdata::io::print_progress_bar(progress); } + printf("\r Frame %d (t=%.1f ps)", frnr + 1, frame_->time); + fflush(stdout); if ((frame_->time >= t_begin_ && (t_end_ < 0 || frame_->time <= t_end_)) && (dt_ == 0 || std::fmod(frame_->time, dt_) == 0) && @@ -443,7 +447,7 @@ class CMData } ++frnr; } - cmdata::io::print_progress_bar(1.f); + printf("\r Read %d frames (t=%.1f ps). \n", frnr, frame_->time); }; if ( intra_ && same_ && cross_) do_run(fi, fs, fc ); diff --git a/tools/cmdata/src/cmdata/frame.hpp b/tools/cmdata/src/cmdata/frame.hpp new file mode 100644 index 00000000..787283db --- /dev/null +++ b/tools/cmdata/src/cmdata/frame.hpp @@ -0,0 +1,101 @@ +#ifndef _CMDATA_FRAME_HPP +#define _CMDATA_FRAME_HPP + +#include +#include +#include +#include +#include + +#ifdef GMXVGE2026 +#include "gromacs/utility/vec.h" +#include "gromacs/utility/vectypes.h" +#else +#include +#include "gromacs/math/vectypes.h" +#endif +#include + +#include "molfile/molfile_plugin.h" + +namespace cmdata { +namespace traj { + +// Status codes returned by Frame::read_next_frame +static constexpr int FRAME_OK = 0; +static constexpr int FRAME_EOF = 1; + +// --------------------------------------------------------------------------- +// Frame — one trajectory frame, backed by a VMD molfile plugin. +// +// coord_scale controls the unit conversion applied to molfile coordinates: +// 1.0f — plugin already outputs nm (GROMACS formats compiled with MOLFILE_NATIVE_NM) +// 0.1f — plugin outputs Angstrom (PDB) +// +// The same scale is applied to box lengths (ts.A/B/C); angles are dimensionless. +// --------------------------------------------------------------------------- +class Frame { +public: + int natom = 0; + float time = 0.f; + float coord_scale = 0.1f; // Angstrom→nm by default; 1.0f when plugin outputs nm + matrix box = {}; + rvec *x = nullptr; + + molfile_plugin_t *mf_plugin = nullptr; + void *mf_handle = nullptr; + float *mf_coords = nullptr; // flat xyzxyz..., in plugin units + + Frame() { x = (rvec *)malloc(0); } + + Frame(int n, molfile_plugin_t *plugin, void *handle, float scale = 0.1f) + : natom(n), coord_scale(scale), mf_plugin(plugin), mf_handle(handle) + { + x = (rvec *)malloc(n * sizeof(rvec)); + mf_coords = (float *)malloc(3 * n * sizeof(float)); + } + + // Convert crystallographic box (lengths in plugin units; angles in degrees) + // to GROMACS triclinic matrix (nm). scale converts lengths to nm. + static void box_from_molfile(float A, float B, float C, + float alpha, float beta, float gamma, + float scale, matrix out) + { + constexpr float d2r = static_cast(M_PI) / 180.f; + const float a = A * scale, b = B * scale, c = C * scale; + const float ca = std::cos(alpha * d2r); + const float cb = std::cos(beta * d2r); + const float cg = std::cos(gamma * d2r); + const float sg = std::sin(gamma * d2r); + + out[0][0] = a; out[0][1] = 0.f; out[0][2] = 0.f; + out[1][0] = b * cg; out[1][1] = b * sg; out[1][2] = 0.f; + out[2][0] = c * cb; out[2][1] = c * (ca - cb * cg) / sg; + const float z2 = c * c - out[2][0] * out[2][0] - out[2][1] * out[2][1]; + out[2][2] = z2 > 0.f ? std::sqrt(z2) : 0.f; + } + + int read_next_frame(bool nopbc, PbcType pbc_type, t_pbc *pbc) + { + molfile_timestep_t ts{}; + ts.coords = mf_coords; + if (mf_plugin->read_next_timestep(mf_handle, natom, &ts) != MOLFILE_SUCCESS) + return FRAME_EOF; + + for (int i = 0; i < natom; i++) + { + x[i][XX] = mf_coords[3 * i ] * coord_scale; + x[i][YY] = mf_coords[3 * i + 1] * coord_scale; + x[i][ZZ] = mf_coords[3 * i + 2] * coord_scale; + } + time = static_cast(ts.physical_time); + box_from_molfile(ts.A, ts.B, ts.C, ts.alpha, ts.beta, ts.gamma, coord_scale, box); + if (!nopbc && pbc) set_pbc(pbc, pbc_type, box); + return FRAME_OK; + } +}; + +} // namespace traj +} // namespace cmdata + +#endif // _CMDATA_FRAME_HPP diff --git a/tools/cmdata/src/cmdata/molfile_support.hpp b/tools/cmdata/src/cmdata/molfile_support.hpp new file mode 100644 index 00000000..32ab767d --- /dev/null +++ b/tools/cmdata/src/cmdata/molfile_support.hpp @@ -0,0 +1,62 @@ +#ifndef _CMDATA_MOLFILE_SUPPORT_HPP +#define _CMDATA_MOLFILE_SUPPORT_HPP + +#include +#include +#include + +#include "molfile/vmdplugin.h" +#include "molfile/molfile_plugin.h" + +// --------------------------------------------------------------------------- +// Declarations for the plugin init/register functions after macro renaming. +// +// Each .cpp is compiled with per-source COMPILE_DEFINITIONS: +// gromacsplugin.cpp: -DVMDPLUGIN_init=molfile_gromacsplugin_init +// -DVMDPLUGIN_register=molfile_gromacsplugin_register +// pdbplugin.cpp: -DVMDPLUGIN_init=molfile_pdbplugin_init +// -DVMDPLUGIN_register=molfile_pdbplugin_register +// --------------------------------------------------------------------------- +int molfile_gromacsplugin_init(); +int molfile_gromacsplugin_register(void *, vmdplugin_register_cb); +int molfile_pdbplugin_init(); +int molfile_pdbplugin_register(void *, vmdplugin_register_cb); + +namespace cmdata { + +// Return the molfile plugin whose filename_extension matches `ext` +// (e.g. "gro", "trr", "pdb"). Initialises all plugins on first call. +inline molfile_plugin_t *get_molfile_plugin(const std::string &ext) +{ + static bool inited = false; + if (!inited) + { + molfile_gromacsplugin_init(); + molfile_pdbplugin_init(); + inited = true; + } + + struct Ctx { const char *ext; molfile_plugin_t *plugin; }; + Ctx ctx{ ext.c_str(), nullptr }; + + // Non-capturing lambda → compatible with vmdplugin_register_cb + auto cb = [](void *data, vmdplugin_t *p) -> int { + auto *c = static_cast(data); + auto *mp = reinterpret_cast(p); + if (mp->filename_extension && + std::strcmp(c->ext, mp->filename_extension) == 0) + c->plugin = mp; + return 0; + }; + + molfile_gromacsplugin_register(&ctx, cb); + molfile_pdbplugin_register(&ctx, cb); + + if (!ctx.plugin) + throw std::runtime_error("No molfile plugin found for extension: " + ext); + return ctx.plugin; +} + +} // namespace cmdata + +#endif // _CMDATA_MOLFILE_SUPPORT_HPP diff --git a/tools/cmdata/src/molfile/COPYRIGHT b/tools/cmdata/src/molfile/COPYRIGHT new file mode 100644 index 00000000..1a92efa0 --- /dev/null +++ b/tools/cmdata/src/molfile/COPYRIGHT @@ -0,0 +1,35 @@ +University of Illinois Open Source License +Copyright 2003 Theoretical and Computational Biophysics Group, +All rights reserved. + +Developed by: Theoretical and Computational Biophysics Group + University of Illinois at Urbana-Champaign + http://www.ks.uiuc.edu/ + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the Software), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to +do so, subject to the following conditions: + +Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimers. + +Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimers in the documentation +and/or other materials provided with the distribution. + +Neither the names of Theoretical and Computational Biophysics Group, +University of Illinois at Urbana-Champaign, nor the names of its contributors +may be used to endorse or promote products derived from this Software without +specific prior written permission. + +THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS WITH THE SOFTWARE. + diff --git a/tools/cmdata/src/molfile/Gromacs.h b/tools/cmdata/src/molfile/Gromacs.h new file mode 100644 index 00000000..4bf6ac82 --- /dev/null +++ b/tools/cmdata/src/molfile/Gromacs.h @@ -0,0 +1,2015 @@ +/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +University of Illinois Open Source License +Copyright 2003 Theoretical and Computational Biophysics Group, +All rights reserved. + +Developed by: Theoretical and Computational Biophysics Group + University of Illinois at Urbana-Champaign + http://www.ks.uiuc.edu/ + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the Software), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to +do so, subject to the following conditions: + +Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimers. + +Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimers in the documentation +and/or other materials provided with the distribution. + +Neither the names of Theoretical and Computational Biophysics Group, +University of Illinois at Urbana-Champaign, nor the names of its contributors +may be used to endorse or promote products derived from this Software without +specific prior written permission. + +THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS WITH THE SOFTWARE. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ +#ifndef __molfile_Gromacs_h +#define __molfile_Gromacs_h +/*************************************************************************** + *cr + *cr (C) Copyright 1995-2016 The Board of Trustees of the + *cr University of Illinois + *cr All Rights Reserved + *cr + ***************************************************************************/ +/*************************************************************************** + * RCS INFORMATION: + * $RCSfile: Gromacs.h,v $ + * $Author: johns $ $Locker: $ $State: Exp $ + * $Revision: 1.36 $ $Date: 2020/01/09 16:21:28 $ + ***************************************************************************/ + +/* + * GROMACS file format reader for VMD + * + * This code provides a high level I/O library for reading + * and writing the following file formats: + * gro GROMACS format or trajectory + * g96 GROMOS-96 format or trajectory + * trj Trajectory - x, v and f (binary, full precision) + * trr Trajectory - x, v and f (binary, full precision, portable) + * xtc Trajectory - x only (compressed, portable, any precision) + * top + * Currently supported: gro trj trr g96 [xtc] + * + * TODO list + * o velocities are ignored because VMD doesn't use them, but some other + * program might ... + * o gro_rec() assumes positions in .gro files are nanometers and + * converts to angstroms, whereas they really could be any unit + */ + +#ifndef GROMACS_H +#define GROMACS_H + +#include +#include +#include +#include +#include + +#if defined(_AIX) +#include +#endif + +#include "endianswap.h" + +#if defined(WIN32) || defined(WIN64) +#define strcasecmp stricmp +#endif + +#ifndef M_PI_2 +#define M_PI_2 1.57079632679489661922 +#endif + +// Error codes for mdio_errno +#define MDIO_SUCCESS 0 +#define MDIO_BADFORMAT 1 +#define MDIO_EOF 2 +#define MDIO_BADPARAMS 3 +#define MDIO_IOERROR 4 +#define MDIO_BADPRECISION 5 +#define MDIO_BADMALLOC 6 +#define MDIO_CANTOPEN 7 +#define MDIO_BADEXTENSION 8 +#define MDIO_UNKNOWNFMT 9 +#define MDIO_CANTCLOSE 10 +#define MDIO_WRONGFORMAT 11 +#define MDIO_SIZEERROR 12 +#define MDIO_UNKNOWNERROR 1000 + +#define MDIO_READ 0 +#define MDIO_WRITE 1 + +#define MDIO_MAX_ERRVAL 11 + +// Format extensions +const char *mdio_fmtexts[] = { + "", + ".gro", + ".trr", + ".g96", + ".trj", + ".xtc", + NULL +}; + + +static int mdio_errcode; // Last error code + +#define TRX_MAGIC 1993 // Magic number for .trX files +#define XTC_MAGIC 1995 // Magic number for .xtc files +#define MAX_GRO_LINE 500 // Maximum line length of .gro files +#define MAX_G96_LINE 500 // Maximum line length of .g96 files +#define MAX_TRX_TITLE 80 // Maximum length of a title in .trX +#define MAX_MDIO_TITLE 80 // Maximum supported title length +// When MOLFILE_NATIVE_NM is defined (set for cmdata's gromacs plugin build) +// coordinates and box lengths stay in nm, avoiding a lossy nm→Å→nm round-trip. +#ifdef MOLFILE_NATIVE_NM +#define ANGS_PER_NM 1 +#define ANGS2_PER_NM2 1 +#else +#define ANGS_PER_NM 10 // Unit conversion factor +#define ANGS2_PER_NM2 100 // Unit conversion factor +#endif + + +// All the supported file types and their respective extensions +#define MDFMT_GRO 1 +#define MDFMT_TRR 2 +#define MDFMT_G96 3 +#define MDFMT_TRJ 4 +#define MDFMT_XTC 5 + + +// A structure to hold .trX file format header information. This +// is an optional member of the md_file structure that is used +// when .trX files are being dealt with. +typedef struct { + int version; // File version number + char title[MAX_TRX_TITLE + 1]; // File title + int ir_size; + int e_size; + int box_size; + int vir_size; + int pres_size; + int top_size; + int sym_size; + int x_size; // Positions of atoms + int v_size; // Velocities of atoms + int f_size; + int natoms; // Number of atoms in the system + int step; + int nre; + float t; + float lambda; +} trx_hdr; + + +// A generic i/o structure that contains information about the +// file itself and the input/output state +typedef struct { + FILE * f; // Pointer to the file + int fmt; // The file format + int prec; // Real number precision + int rev; // Reverse endiannism? + trx_hdr * trx; // Trx files require a great deal more + // header data to be stored. +} md_file; + + +// A format-independent structure to hold header data from files +typedef struct { + char title[MAX_MDIO_TITLE + 1]; + int natoms; + float timeval; +} md_header; + + +// A format-independent structure to hold unit cell data +typedef struct { + float A, B, C, alpha, beta, gamma; +} md_box; + + +// Timestep information +typedef struct { + float *pos; // Position array (3 * natoms) + //float *vel; // Velocity array ** (VMD doesn't use this) ** + //float *f; // Force array ** (VMD doesn't use this) ** + //float *box; // Computational box ** (VMD doesn't use this) ** + int natoms; // Number of atoms + int step; // Simulation step + float time; // Time of simulation + md_box *box; +} md_ts; + + +// Atom information +typedef struct { + char resid[7]; // Residue index number + char resname[7]; // Residue name + int atomnum; // Atom index number + char atomname[7]; // Atom name + float pos[3]; // Position array (3 * natoms) + //float vel[3]; // Velocity array ** (VMD doesn't use this) ** +} md_atom; + + +// Open a molecular dynamics file. The second parameter specifies +// the format of the file. If it is zero, the format is determined +// from the file extension. the third argument (if given) decides +// whether to read (==0) or to write (!= 0). +// using a default argument set to read for backward compatibility. +static md_file *mdio_open(const char *, const int, const int=MDIO_READ); + +// Closes a molecular dynamics file. +static int mdio_close(md_file *); + + +// Format-independent file I/O routines +static int mdio_header(md_file *, md_header *); +static int mdio_timestep(md_file *, md_ts *); + + +// .gro file functions +static int gro_header(md_file *, char *, int, float *, int *, int = 1); +static int gro_rec(md_file *, md_atom *); +static int gro_timestep(md_file *, md_ts *); + + +// .trX file functions +static int trx_header(md_file *, int = 0); +static int trx_int(md_file *, int *); +static int trx_real(md_file *, float *); + +static int trx_rvector(md_file *, float *); +static int trx_string(md_file *, char *, int); +static int trx_timestep(md_file *, md_ts *); + +// .g96 file functions +static int g96_header(md_file *, char *, int, float *); +static int g96_timestep(md_file *, md_ts *); +static int g96_rec(md_file *, md_atom *); +static int g96_countatoms(md_file *); + + +// .xtc file functions +static int xtc_int(md_file *, int *); +static int xtc_float(md_file *, float *); +/* +static int xtc_receivebits(int *, int); +static void xtc_receiveints(int *, int, int, const unsigned *, int *); +*/ +static int xtc_timestep(md_file *, md_ts *); +static int xtc_3dfcoord(md_file *, float *, int *, float *); + + +// Error reporting functions +static int mdio_errno(void); +static const char *mdio_errmsg(int); +static int mdio_seterror(int); + + +// Miscellaneous functions +static int strip_white(char *); +static int mdio_readline(md_file *, char *, int, int = 1); +static int mdio_tsfree(md_ts *, int = 0); +static int mdio_readbox(md_box *, float *, float *, float *); + + + +static int xtc_receivebits(int *, int); + +// Error descriptions for mdio_errno +static const char *mdio_errdescs[] = { + "no error", + "file does not match format", + "unexpected end-of-file reached", + "function called with bad parameters", + "file i/o error", + "unsupported precision", + "out of memory", + "cannot open file", + "bad file extension", + "unknown file format", + "cannot close file", + "wrong file format for this function", + "binary i/o error: sizeof(int) != 4", + NULL +}; + +/*! \fn static inline bool host_is_little_endian(void) + * detect endiannes of host machine. returns true on little endian machines. */ +static inline int host_is_little_endian(void) +{ + const union { unsigned char c[4]; unsigned int i; } + fixed = { { 0x10 , 0x20 , 0x40 , 0x80 } }; + const unsigned int i = 0x80402010U; + + if (fixed.i == i) { + return 1; + } + return 0; +} + + + +// Open a molecular dynamics file. The second parameter specifies +// the format of the file. If it is zero, the format is determined +// from the file extension. +md_file *mdio_open(const char *fn, const int fmt, const int rw) { + md_file *mf; + + if (!fn) { + mdio_seterror(MDIO_BADPARAMS); + return NULL; + } + + // Allocate memory + mf = (md_file *) malloc(sizeof(md_file)); + if (!mf) { + mdio_seterror(MDIO_BADMALLOC); + return NULL; + } + + // Zero out the structure + memset(mf, 0, sizeof(md_file)); + + // Determine the file type from the extension + if (!fmt) { + char *p; + int n; + + // Seek to the extension part of the filename + for (p = (char *) &fn[strlen(fn) - 1]; *p != '.' && p > fn; p--); + if (p == fn) { + free(mf); + mdio_seterror(MDIO_BADEXTENSION); + return NULL; + } + + // Check the extension against known extensions + for (n = 1; mdio_fmtexts[n]; n++) + if (!strcasecmp(p, mdio_fmtexts[n])) break; + + // If !mdio_fmtexts[n], we failed (unknown ext) + if (!mdio_fmtexts[n]) { + free(mf); + mdio_seterror(MDIO_UNKNOWNFMT); + return NULL; + } + + // All set + mf->fmt = n; + } + else { + mf->fmt = fmt; + } + + // Differentiate between binary and ascii files. Also, + // .trX files need a header information structure allocated. + switch (mf->fmt) { + case MDFMT_GRO: + case MDFMT_G96: /* fallthrough */ + if (rw) + mf->f = fopen(fn, "wt"); + else + mf->f = fopen(fn, "rt"); + + break; + case MDFMT_TRR: + case MDFMT_TRJ: /* fallthrough */ + // Allocate the trx header data struct + mf->trx = (trx_hdr *) malloc(sizeof(trx_hdr)); + if (!mf->trx) { + free(mf); + mdio_seterror(MDIO_BADMALLOC); + return NULL; + } + memset(mf->trx, 0, sizeof(trx_hdr)); + case MDFMT_XTC: /* fallthrough */ + // Finally, open the file + if (rw) + mf->f = fopen(fn, "wb"); + else + mf->f = fopen(fn, "rb"); + + break; + default: + free(mf); + mdio_seterror(MDIO_UNKNOWNFMT); + return NULL; + } + + // Check for opening error + if (!mf->f) { + if (mf->trx) free(mf->trx); + free(mf); + mdio_seterror(MDIO_CANTOPEN); + return NULL; + } + + // File is opened, we're all set! + mdio_seterror(MDIO_SUCCESS); + return mf; +} + + +// Closes a molecular dynamics file. +static int mdio_close(md_file *mf) { + if (!mf) return mdio_seterror(MDIO_BADPARAMS); + + if (fclose(mf->f) == EOF) return mdio_seterror(MDIO_CANTCLOSE); + + // Free the dynamically allocated memory + if (mf->trx) free(mf->trx); + free(mf); + return mdio_seterror(MDIO_SUCCESS); +} + + +// Returns the last error code reported by any of the mdio functions +static int mdio_errno(void) { + return mdio_errcode; +} + + +// Returns a textual message regarding an mdio error code +static const char *mdio_errmsg(int n) { + if (n < 0 || n > MDIO_MAX_ERRVAL) return (char *) "unknown error"; + else return mdio_errdescs[n]; +} + + +// Sets the error code and returns an appropriate return value +// for the calling function to return to its parent +static int mdio_seterror(int code) { + mdio_errcode = code; + return code ? -1 : 0; +} + + +// Reads a line from the text file, strips leading/trailing whitespace +// and newline, checks for errors, and returns the number of characters +// in the string on success or -1 on error. +static int mdio_readline(md_file *mf, char *buf, int n, int strip) { + if (!buf || n < 1 || !mf) return mdio_seterror(MDIO_BADPARAMS); + + // Read the line + fgets(buf, n, mf->f); + + // End of file reached? + if (feof(mf->f)) return mdio_seterror(MDIO_EOF); + + // File I/O error? + if (ferror(mf->f)) return mdio_seterror(MDIO_IOERROR); + + // comment line? + if (buf[0] == '#') return mdio_readline(mf,buf,n,strip); + + // Strip whitespace + if (strip) strip_white(buf); + + return strlen(buf); +} + + +// Strips leading and trailing whitespace from a string. Tabs, +// spaces, newlines and carriage returns are stripped. Example: +// "\n hello\t \r" becomes "hello". +static int strip_white(char *buf) { + int i, j, k; + + // Protect against NULL pointer + if (!buf) return -1; + if (!strlen(buf)) return -1; + + // Kill trailing whitespace first + for (i = strlen(buf) - 1; + buf[i] == ' ' || buf[i] == '\t' || + buf[i] == '\n' || buf[i] == '\r'; + i--) + buf[i] = 0; + + // Skip past leading whitespace + for (i = 0; buf[i] == ' ' || buf[i] == '\t' || + buf[i] == '\n' || buf[i] == '\r'; i++); + if (i) { + k = 0; + for (j = i; buf[j]; j++) + buf[k++] = buf[j]; + buf[k] = 0; + } + + return strlen(buf); +} + + +// Frees the memory allocated in a ts structure. The holderror +// parameter defaults to zero. Programs that are calling this +// function because of an error reported by another function should +// set holderror so that mdio_tsfree() does not overwrite the error +// code with mdio_seterror(). +static int mdio_tsfree(md_ts *ts, int holderror) { + if (!ts) { + if (holderror) return -1; + else return mdio_seterror(MDIO_BADPARAMS); + } + + if (ts->pos && ts->natoms > 0) free(ts->pos); + + if (ts->box) free(ts->box); + + if (holderror) return -1; + else return mdio_seterror(MDIO_SUCCESS); +} + + +// Converts box basis vectors to A, B, C, alpha, beta, and gamma. +// Stores values in md_box struct, which should be allocated before calling +// this function. +static int mdio_readbox(md_box *box, float *x, float *y, float *z) { + float A, B, C; + + if (!box) { + return mdio_seterror(MDIO_BADPARAMS); + } + + // A, B, C are the lengths of the x, y, z vectors, respectively + A = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] ) * ANGS_PER_NM; + B = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] ) * ANGS_PER_NM; + C = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] ) * ANGS_PER_NM; + if ((A<=0) || (B<=0) || (C<=0)) { + /* Use zero-length box size and set angles to 90. */ + box->A = box->B = box->C = 0; + box->alpha = box->beta = box->gamma = 90; + } else { + box->A = A; + box->B = B; + box->C = C; + + // gamma, beta, alpha are the angles between the x & y, x & z, y & z + // vectors, respectively + box->gamma = acos( (x[0]*y[0]+x[1]*y[1]+x[2]*y[2])*ANGS2_PER_NM2/(A*B) ) * 90.0/M_PI_2; + box->beta = acos( (x[0]*z[0]+x[1]*z[1]+x[2]*z[2])*ANGS2_PER_NM2/(A*C) ) * 90.0/M_PI_2; + box->alpha = acos( (y[0]*z[0]+y[1]*z[1]+y[2]*z[2])*ANGS2_PER_NM2/(B*C) ) * 90.0/M_PI_2; + } + return mdio_seterror(MDIO_SUCCESS); +} + + +// Reads the header of a file (format independent) +static int mdio_header(md_file *mf, md_header *mdh) { + int n; + if (!mf || !mdh) return mdio_seterror(MDIO_BADPARAMS); + if (!mf->f) return mdio_seterror(MDIO_BADPARAMS); + + switch (mf->fmt) { + case MDFMT_GRO: + if (gro_header(mf, mdh->title, MAX_MDIO_TITLE, + &mdh->timeval, &mdh->natoms, 1) < 0) + return -1; + return 0; + + case MDFMT_TRR: + case MDFMT_TRJ: /* fallthrough */ + if (trx_header(mf, 1) < 0) return -1; + mdh->natoms = mf->trx->natoms; + mdh->timeval = (float) mf->trx->t; + strncpy(mdh->title, mf->trx->title, MAX_MDIO_TITLE); + return 0; + + case MDFMT_G96: + if (g96_header(mf, mdh->title, MAX_MDIO_TITLE, + &mdh->timeval) < 0) return -1; + mdh->natoms = -1; + return 0; + + case MDFMT_XTC: + memset(mdh, 0, sizeof(md_header)); + // Check magic number + if (xtc_int(mf, &n) < 0) return -1; + if (n != XTC_MAGIC) return mdio_seterror(MDIO_BADFORMAT); + + // Get number of atoms + if (xtc_int(mf, &n) < 0) return -1; + mdh->natoms = n; + rewind(mf->f); + return 0; + + default: + return mdio_seterror(MDIO_UNKNOWNFMT); + } +} + + +// Reads in a timestep from a file (format independent) +static int mdio_timestep(md_file *mf, md_ts *ts) { + if (!mf || !ts) return mdio_seterror(MDIO_BADPARAMS); + if (!mf->f) return mdio_seterror(MDIO_BADPARAMS); + + switch (mf->fmt) { + case MDFMT_GRO: + return gro_timestep(mf, ts); + + case MDFMT_TRR: + case MDFMT_TRJ: /* fallthrough */ + return trx_timestep(mf, ts); + + case MDFMT_G96: + return g96_timestep(mf, ts); + + case MDFMT_XTC: + return xtc_timestep(mf, ts); + + default: + return mdio_seterror(MDIO_UNKNOWNFMT); + } +} + + + +static int g96_header(md_file *mf, char *title, int titlelen, float *timeval) { + char buf[MAX_G96_LINE + 1]; + char *p; + + // Check parameters + if (!mf) return mdio_seterror(MDIO_BADPARAMS); + + // The header consists of blocks. The title block + // is mandatory, and a TIMESTEP block is optional. + // Example: + // + // TITLE + // Generated by trjconv : t= 90.00000 + // more title info + // . + // . + // . + // END + // . + // . + // . + + if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1; + if (strcasecmp(buf, "TITLE")) return mdio_seterror(MDIO_BADFORMAT); + + // Read in the title itself + if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1; + + // The timevalue can be included in the title string + // after a "t=" prefix. + if ((p = (char *) strstr(buf, "t="))) { + char *q = p; + *(q--) = 0; + + // Skip the `t=' and strip whitespace from + // the resulting strings + p += 2; + strip_white(p); + strip_white(buf); + + // Grab the timevalue from the title string + if (timeval) *timeval = (float) atof(p); + } + else { + // No timevalue - just copy the string and strip + // any leading/trailing whitespace + if (timeval) *timeval = 0; + strip_white(buf); + } + + // Copy the title string + if (title && titlelen) strncpy(title, buf, titlelen); + + // Now ignore subsequent title lines and get the END string + while (strcasecmp(buf, "END")) + if (mdio_readline(mf, buf, MAX_G96_LINE + 1) < 0) return -1; + + // Done! + return mdio_seterror(MDIO_SUCCESS); +} + + +// Used to determine the number of atoms in a g96 file, because +// VMD needs to know this for some reason. +static int g96_countatoms(md_file *mf) { + char buf[MAX_G96_LINE + 1]; + int natoms; + int n; + long fpos; + float lastf; + + if (!mf) return mdio_seterror(MDIO_BADPARAMS); + + fpos = ftell(mf->f); + + natoms = 0; + for (;;) { + if (mdio_readline(mf, buf, MAX_G96_LINE + 1, 0) < 0) + break; + n = sscanf(buf, "%*6c%*6c%*6c%*6c %*f %*f %f", &lastf); + if (n == 1) natoms++; + else { + strip_white(buf); + if (!strcasecmp(buf, "END")) break; + } + } + + fseek(mf->f, fpos, SEEK_SET); + return natoms; +} + + +// Reads an atom line from the G96 file +static int g96_rec(md_file *mf, md_atom *ma) { + char buf[MAX_G96_LINE + 1]; + char atomnum[7]; + int n; + + // Check parameters + if (!mf || !ma) return mdio_seterror(MDIO_BADPARAMS); + + // Read in a line, assuming it is an atom line + do { + if (mdio_readline(mf, buf, MAX_G96_LINE + 1, 0) < 0) return -1; + } while (buf[0] == '#' || strlen(buf) == 0); + + n = sscanf(buf, "%6c%6c%6c%6c %f %f %f", + ma->resid, ma->resname, ma->atomname, atomnum, + &ma->pos[0], &ma->pos[1], &ma->pos[2]); + if (n == 7) { + atomnum[6] = 0; + ma->resid[6] = 0; + ma->resname[6] = 0; + ma->atomname[6] = 0; + + strip_white(atomnum); + strip_white(ma->resid); + strip_white(ma->resname); + strip_white(ma->atomname); + + ma->atomnum = atoi(atomnum); + + ma->pos[0] *= ANGS_PER_NM; + ma->pos[1] *= ANGS_PER_NM; + ma->pos[2] *= ANGS_PER_NM; + + return 0; + } + + return mdio_seterror(MDIO_BADFORMAT); +} + + +// Reads a timestep from a G96 file and stores the data in +// the generic md_ts structure. Returns 0 on success or a +// negative number on error and sets mdio_errcode. +static int g96_timestep(md_file *mf, md_ts *ts) { + char buf[MAX_G96_LINE + 1]; + char stripbuf[MAX_G96_LINE + 1]; + float pos[3], x[3], y[3], z[3], *currAtom; + long fpos; + int n, i, boxItems; + + // Check parameters + if (!mf || !ts) return mdio_seterror(MDIO_BADPARAMS); + + // Allocate data space for the timestep, using the number of atoms + // determined by open_g96_read(). + ts->pos = (float *) malloc(sizeof(float) * 3 * ts->natoms); + if (!ts->pos) { + return mdio_seterror(MDIO_BADMALLOC); + } + currAtom = ts->pos; + + // The timesteps follow the header in a fixed block + // format: + // + // TIMESTEP + //