Update instructions in containers.rst
[gromacs.git] / src / gromacs / coordinateio / outputadapters / outputselector.cpp
blob375c51c6d418939d6e1863a682130951512e556a
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*!\internal
36 * \file
37 * \brief
38 * Implements outputselector class.
40 * \author Paul Bauer <paul.bauer.q@gmail.com>
41 * \ingroup module_coordinateio
44 #include "gmxpre.h"
46 #include "outputselector.h"
48 #include <algorithm>
50 #include "gromacs/math/vec.h"
51 #include "gromacs/trajectory/trajectoryframe.h"
52 #include "gromacs/utility/smalloc.h"
54 namespace gmx
57 /*! \brief
58 * Modify atoms information in coordinate frame to fit output selection.
60 * Changes the information contained in the coordinate frame t_atoms struct to match
61 * the selection provided to the module.
63 * \param[in] atoms Pointer to original t_atoms.
64 * \param[in] selectionAtoms Pointer to local atoms.
65 * \param[in] sel Reference to selection.
67 static void adjustAtomInformation(t_atoms* atoms, t_atoms* selectionAtoms, const Selection& sel)
69 int natoms = sel.atomCount();
71 selectionAtoms->nr = natoms;
72 selectionAtoms->nres = atoms->nres;
74 srenew(selectionAtoms->resinfo, atoms->nres);
75 srenew(selectionAtoms->atom, natoms);
76 srenew(selectionAtoms->atomname, natoms);
78 selectionAtoms->haveType = atoms->haveType;
79 selectionAtoms->haveBState = atoms->haveBState;
80 selectionAtoms->haveMass = atoms->haveMass;
81 selectionAtoms->haveCharge = atoms->haveCharge;
82 selectionAtoms->havePdbInfo = atoms->havePdbInfo;
84 if (atoms->haveType)
86 srenew(selectionAtoms->atomtype, natoms);
88 if (atoms->haveBState)
90 srenew(selectionAtoms->atomtypeB, natoms);
92 if (atoms->havePdbInfo)
94 srenew(selectionAtoms->pdbinfo, natoms);
97 for (int i = 0; i < natoms; i++)
99 int pos = sel.position(i).refId();
100 selectionAtoms->atom[i] = atoms->atom[pos];
101 selectionAtoms->atomname[i] = atoms->atomname[pos];
102 if (selectionAtoms->haveType)
104 selectionAtoms->atomtype[i] = atoms->atomtype[pos];
106 if (selectionAtoms->haveBState)
108 selectionAtoms->atomtypeB[i] = atoms->atomtypeB[pos];
110 if (selectionAtoms->havePdbInfo)
112 selectionAtoms->pdbinfo[i] = atoms->pdbinfo[pos];
115 // Copy residue information unconditionally.
116 for (int i = 0; i < atoms->nres; i++)
118 selectionAtoms->resinfo[i] = atoms->resinfo[i];
122 void OutputSelector::processFrame(const int /*framenumber*/, t_trxframe* input)
124 size_t natoms = sel_.atomCount();
126 input->natoms = natoms;
128 localX_.resize(natoms);
129 if (input->bV)
131 localV_.resize(natoms);
133 if (input->bF)
135 localF_.resize(natoms);
137 if (input->index)
139 localIndex_.resize(natoms);
142 for (size_t i = 0; i < natoms; i++)
144 int pos = sel_.position(i).refId();
146 copy_rvec(input->x[pos], localX_[i]);
147 if (input->bV)
149 copy_rvec(input->v[pos], localV_[i]);
151 if (input->bF)
153 copy_rvec(input->f[pos], localF_[i]);
155 if (input->index)
157 localIndex_[i] = input->index[pos];
160 input->x = as_rvec_array(localX_.data());
162 input->index = localIndex_.data();
164 if (input->bV)
166 input->v = as_rvec_array(localV_.data());
168 if (input->bF)
170 input->f = as_rvec_array(localF_.data());
173 if (input->bAtoms)
175 if (selectionAtoms_ == nullptr)
177 selectionAtoms_.reset(new t_atoms);
178 init_t_atoms(selectionAtoms_.get(), natoms, false);
180 adjustAtomInformation(input->atoms, selectionAtoms_.get(), sel_);
181 input->atoms = selectionAtoms_.get();
185 } // namespace gmx