Update instructions in containers.rst
[gromacs.git] / src / gromacs / math / coordinatetransformation.cpp
blob6555d96e2db2e072e0e8f9374ad04e3582e3f3c1
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019,2020, 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 \file
36 * \brief Implements coordinate transformation routines.
38 * \author Christian Blau <blau@kth.se>
40 #include "gmxpre.h"
42 #include "coordinatetransformation.h"
44 #include <vector>
46 #include "gromacs/math/vec.h"
47 #include "gromacs/mdspan/extensions.h"
48 #include "gromacs/utility/arrayref.h"
50 #include "matrix.h"
52 namespace gmx
55 /********************************************************************
56 * ScaleCoordinates::Impl
59 class ScaleCoordinates::Impl
61 public:
62 Impl(const RVec& scale);
63 void inverseIgnoringZeroScale(ArrayRef<RVec> coordinates) const;
64 void scale(ArrayRef<RVec> coordinates) const;
66 private:
67 RVec scale_;
69 ScaleCoordinates::Impl::Impl(const RVec& scale) : scale_{ scale } {}
70 void ScaleCoordinates::Impl::scale(ArrayRef<RVec> coordinates) const
72 for (auto& coordinate : coordinates)
74 coordinate[XX] *= scale_[XX];
75 coordinate[YY] *= scale_[YY];
76 coordinate[ZZ] *= scale_[ZZ];
80 void ScaleCoordinates::Impl::inverseIgnoringZeroScale(ArrayRef<RVec> coordinates) const
82 RVec inverseScale;
83 for (int dimension = XX; dimension <= ZZ; ++dimension)
85 inverseScale[dimension] = scale_[dimension] != 0 ? 1. / scale_[dimension] : 1.;
88 for (auto& coordinate : coordinates)
90 coordinate[XX] *= inverseScale[XX];
91 coordinate[YY] *= inverseScale[YY];
92 coordinate[ZZ] *= inverseScale[ZZ];
96 /********************************************************************
97 * ScaleCoordinates
100 ScaleCoordinates::ScaleCoordinates(const RVec& scale) : impl_{ new Impl(scale) } {}
102 void ScaleCoordinates::operator()(ArrayRef<RVec> coordinates) const
104 impl_->scale(coordinates);
107 void ScaleCoordinates::operator()(RVec* coordinate) const
109 impl_->scale({ coordinate, coordinate + 1 });
112 void ScaleCoordinates::inverseIgnoringZeroScale(ArrayRef<RVec> coordinates) const
114 impl_->inverseIgnoringZeroScale(coordinates);
117 void ScaleCoordinates::inverseIgnoringZeroScale(RVec* coordinate) const
119 impl_->inverseIgnoringZeroScale({ coordinate, coordinate + 1 });
122 ScaleCoordinates::~ScaleCoordinates() = default;
124 ScaleCoordinates::ScaleCoordinates(const ScaleCoordinates& other) : impl_(new Impl(*other.impl_)) {}
126 ScaleCoordinates& ScaleCoordinates::operator=(const ScaleCoordinates& other)
128 *impl_ = *other.impl_;
129 return *this;
132 ScaleCoordinates::ScaleCoordinates(ScaleCoordinates&&) noexcept = default;
134 ScaleCoordinates& ScaleCoordinates::operator=(ScaleCoordinates&&) noexcept = default;
136 /********************************************************************
137 * TranslateAndScale::Impl
140 class TranslateAndScale::Impl
142 public:
143 Impl(const RVec& scale, const RVec& translation);
144 void transform(ArrayRef<RVec> coordinates) const;
145 RVec scale_;
146 RVec translation_;
149 TranslateAndScale::Impl::Impl(const RVec& scale, const RVec& translation) :
150 scale_{ scale },
151 translation_{ translation }
155 void TranslateAndScale::Impl::transform(ArrayRef<RVec> coordinates) const
157 for (auto& coordinate : coordinates)
159 coordinate += translation_;
160 coordinate[XX] *= scale_[XX];
161 coordinate[YY] *= scale_[YY];
162 coordinate[ZZ] *= scale_[ZZ];
167 /********************************************************************
168 * TranslateAndScale
171 TranslateAndScale::TranslateAndScale(const RVec& scale, const RVec& translation) :
172 impl_(new Impl(scale, translation))
176 void TranslateAndScale::operator()(ArrayRef<RVec> coordinates) const
178 impl_->transform(coordinates);
181 void TranslateAndScale::operator()(RVec* coordinate) const
183 impl_->transform({ coordinate, coordinate + 1 });
186 ScaleCoordinates TranslateAndScale::scaleOperationOnly() const
188 return ScaleCoordinates{ impl_->scale_ };
191 TranslateAndScale::~TranslateAndScale() = default;
193 TranslateAndScale::TranslateAndScale(const TranslateAndScale& other) : impl_(new Impl(*other.impl_))
197 TranslateAndScale& TranslateAndScale::operator=(const TranslateAndScale& other)
199 *impl_ = *other.impl_;
200 return *this;
203 TranslateAndScale::TranslateAndScale(TranslateAndScale&&) noexcept = default;
205 TranslateAndScale& TranslateAndScale::operator=(TranslateAndScale&&) noexcept = default;
207 /********************************************************************
208 * AffineTransformation
211 AffineTransformation::AffineTransformation(Matrix3x3ConstSpan matrix, const RVec& translation) :
212 translation_{ translation }
214 std::copy(begin(matrix), end(matrix), begin(matrix_));
217 void AffineTransformation::operator()(ArrayRef<RVec> vectors) const
219 for (RVec& vector : vectors)
221 matrixVectorMultiply(matrix_.asConstView(), &vector);
222 vector += translation_;
226 void AffineTransformation::operator()(RVec* vector) const
228 (*this)({ vector, vector + 1 });
231 } // namespace gmx