1 /*
2 * This file is part of the GROMACS molecular simulation package.
3 *
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.
8 *
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.
13 *
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.
18 *
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.
23 *
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.
31 *
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.
34 */
35 /*! \internal \file
36 * \brief Implements coordinate transformation routines.
37 *
38 * \author Christian Blau <blau@kth.se>
39 */
40 #include "gmxpre.h"
41
42 #include "coordinatetransformation.h"
43
44 #include <vector>
45
46 #include "gromacs/math/vec.h"
47 #include "gromacs/mdspan/extensions.h"
48 #include "gromacs/utility/arrayref.h"
49
50 #include "matrix.h"
51
52 namespace gmx
53 {
54
55 /********************************************************************
56 * ScaleCoordinates::Impl
57 */
58
59 class ScaleCoordinates::Impl
60 {
61 public:
62 Impl(const RVec& scale);
63 void inverseIgnoringZeroScale(ArrayRef<RVec> coordinates) const;
64 void scale(ArrayRef<RVec> coordinates) const;
65
66 private:
67 RVec scale_;
68 };
Impl(const RVec & scale)69 ScaleCoordinates::Impl::Impl(const RVec& scale) : scale_{ scale } {}
scale(ArrayRef<RVec> coordinates) const70 void ScaleCoordinates::Impl::scale(ArrayRef<RVec> coordinates) const
71 {
72 for (auto& coordinate : coordinates)
73 {
74 coordinate[XX] *= scale_[XX];
75 coordinate[YY] *= scale_[YY];
76 coordinate[ZZ] *= scale_[ZZ];
77 }
78 }
79
inverseIgnoringZeroScale(ArrayRef<RVec> coordinates) const80 void ScaleCoordinates::Impl::inverseIgnoringZeroScale(ArrayRef<RVec> coordinates) const
81 {
82 RVec inverseScale;
83 for (int dimension = XX; dimension <= ZZ; ++dimension)
84 {
85 inverseScale[dimension] = scale_[dimension] != 0 ? 1. / scale_[dimension] : 1.;
86 }
87
88 for (auto& coordinate : coordinates)
89 {
90 coordinate[XX] *= inverseScale[XX];
91 coordinate[YY] *= inverseScale[YY];
92 coordinate[ZZ] *= inverseScale[ZZ];
93 }
94 }
95
96 /********************************************************************
97 * ScaleCoordinates
98 */
99
ScaleCoordinates(const RVec & scale)100 ScaleCoordinates::ScaleCoordinates(const RVec& scale) : impl_{ new Impl(scale) } {}
101
operator ()(ArrayRef<RVec> coordinates) const102 void ScaleCoordinates::operator()(ArrayRef<RVec> coordinates) const
103 {
104 impl_->scale(coordinates);
105 }
106
operator ()(RVec * coordinate) const107 void ScaleCoordinates::operator()(RVec* coordinate) const
108 {
109 impl_->scale({ coordinate, coordinate + 1 });
110 }
111
inverseIgnoringZeroScale(ArrayRef<RVec> coordinates) const112 void ScaleCoordinates::inverseIgnoringZeroScale(ArrayRef<RVec> coordinates) const
113 {
114 impl_->inverseIgnoringZeroScale(coordinates);
115 }
116
inverseIgnoringZeroScale(RVec * coordinate) const117 void ScaleCoordinates::inverseIgnoringZeroScale(RVec* coordinate) const
118 {
119 impl_->inverseIgnoringZeroScale({ coordinate, coordinate + 1 });
120 }
121
122 ScaleCoordinates::~ScaleCoordinates() = default;
123
ScaleCoordinates(const ScaleCoordinates & other)124 ScaleCoordinates::ScaleCoordinates(const ScaleCoordinates& other) : impl_(new Impl(*other.impl_)) {}
125
operator =(const ScaleCoordinates & other)126 ScaleCoordinates& ScaleCoordinates::operator=(const ScaleCoordinates& other)
127 {
128 *impl_ = *other.impl_;
129 return *this;
130 }
131
132 ScaleCoordinates::ScaleCoordinates(ScaleCoordinates&&) noexcept = default;
133
134 ScaleCoordinates& ScaleCoordinates::operator=(ScaleCoordinates&&) noexcept = default;
135
136 /********************************************************************
137 * TranslateAndScale::Impl
138 */
139
140 class TranslateAndScale::Impl
141 {
142 public:
143 Impl(const RVec& scale, const RVec& translation);
144 void transform(ArrayRef<RVec> coordinates) const;
145 RVec scale_;
146 RVec translation_;
147 };
148
Impl(const RVec & scale,const RVec & translation)149 TranslateAndScale::Impl::Impl(const RVec& scale, const RVec& translation) :
150 scale_{ scale },
151 translation_{ translation }
152 {
153 }
154
transform(ArrayRef<RVec> coordinates) const155 void TranslateAndScale::Impl::transform(ArrayRef<RVec> coordinates) const
156 {
157 for (auto& coordinate : coordinates)
158 {
159 coordinate += translation_;
160 coordinate[XX] *= scale_[XX];
161 coordinate[YY] *= scale_[YY];
162 coordinate[ZZ] *= scale_[ZZ];
163 }
164 }
165
166
167 /********************************************************************
168 * TranslateAndScale
169 */
170
TranslateAndScale(const RVec & scale,const RVec & translation)171 TranslateAndScale::TranslateAndScale(const RVec& scale, const RVec& translation) :
172 impl_(new Impl(scale, translation))
173 {
174 }
175
operator ()(ArrayRef<RVec> coordinates) const176 void TranslateAndScale::operator()(ArrayRef<RVec> coordinates) const
177 {
178 impl_->transform(coordinates);
179 }
180
operator ()(RVec * coordinate) const181 void TranslateAndScale::operator()(RVec* coordinate) const
182 {
183 impl_->transform({ coordinate, coordinate + 1 });
184 }
185
scaleOperationOnly() const186 ScaleCoordinates TranslateAndScale::scaleOperationOnly() const
187 {
188 return ScaleCoordinates{ impl_->scale_ };
189 }
190
191 TranslateAndScale::~TranslateAndScale() = default;
192
TranslateAndScale(const TranslateAndScale & other)193 TranslateAndScale::TranslateAndScale(const TranslateAndScale& other) : impl_(new Impl(*other.impl_))
194 {
195 }
196
operator =(const TranslateAndScale & other)197 TranslateAndScale& TranslateAndScale::operator=(const TranslateAndScale& other)
198 {
199 *impl_ = *other.impl_;
200 return *this;
201 }
202
203 TranslateAndScale::TranslateAndScale(TranslateAndScale&&) noexcept = default;
204
205 TranslateAndScale& TranslateAndScale::operator=(TranslateAndScale&&) noexcept = default;
206
207 /********************************************************************
208 * AffineTransformation
209 */
210
AffineTransformation(Matrix3x3ConstSpan matrix,const RVec & translation)211 AffineTransformation::AffineTransformation(Matrix3x3ConstSpan matrix, const RVec& translation) :
212 translation_{ translation }
213 {
214 std::copy(begin(matrix), end(matrix), begin(matrix_));
215 }
216
operator ()(ArrayRef<RVec> vectors) const217 void AffineTransformation::operator()(ArrayRef<RVec> vectors) const
218 {
219 for (RVec& vector : vectors)
220 {
221 matrixVectorMultiply(matrix_.asConstView(), &vector);
222 vector += translation_;
223 }
224 }
225
operator ()(RVec * vector) const226 void AffineTransformation::operator()(RVec* vector) const
227 {
228 (*this)({ vector, vector + 1 });
229 }
230
231 } // namespace gmx
232