VectorSpaceOps.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::VectorSpaceOps
29 
30 Description
31  Operator functions for VectorSpace.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef Foam_VectorSpaceOps_H
36 #define Foam_VectorSpaceOps_H
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
40 namespace Foam
41 {
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 //- Recursive execution. Terminating at <N>, starting at index <I>
46 template<direction N, direction I=0>
47 struct VectorSpaceOps
48 {
49  //- Somewhat equivalent to std::copy_n() but with templated loops.
50  // \param [in] input indexable input data
51  // \param [out] result indexable output data
52  template<class Input, class Output>
53  static inline void copy_n(Input input, Output result)
54  {
55  // if constexpr (I < N)
56  {
57  result[I] = input[I];
59  }
60  }
61 
62  //- Somewhat equivalent to std::fill_n() but with templated loops
63  // \param [out] result indexable output data
64  // \param val the value to assign for each entry
65  template<class Output, class T>
66  static inline void fill_n(Output result, const T& val)
67  {
68  // if constexpr (I < N)
69  {
70  result[I] = val;
71  VectorSpaceOps<N, I+1>::fill_n(result, val);
72  }
73  }
74 
75  //- Apply the binary assignment operation to each vector-space
76  //- component.
77  // \param [in,out] vs vector-space (indexed) data
78  // \param s scalar/component data (non-indexed)
79  // \param eo binary combine/assign operation
80  template<class V, class S, class EqOp>
81  static inline void eqOpS(V& vs, const S& s, EqOp eo)
82  {
83  // if constexpr (I < N)
84  {
85  eo(vs.v_[I], s);
87  }
88  }
89 
90  //- Apply the inplace binary reduction operation.
91  // \param [in,out] s scalar or component data (non-indexed)
92  // \param [in] vs input vector-space (indexed) data
93  // \param eo binary combine/assign operation
94  template<class S, class V, class EqOp>
95  static inline void SeqOp(S& s, const V& vs, EqOp eo)
96  {
97  // if constexpr (I < N)
98  {
99  eo(s, vs.v_[I]);
101  }
102  }
103 
104  //- Apply the inplace binary assignment operation to the components.
105  // \param [in,out] vs1 vector-space (indexed) data
106  // \param [in] vs2 second vector-space (indexed) data
107  // \param eo binary combine/assign operation
108  template<class V1, class V2, class EqOp>
109  static inline void eqOp(V1& vs1, const V2& vs2, EqOp eo)
110  {
111  // if constexpr (I < N)
112  {
113  eo(vs1.v_[I], vs2.v_[I]);
114  VectorSpaceOps<N, I+1>::eqOp(vs1, vs2, eo);
115  }
116  }
117 
118  //- Apply the binary operation between vector-space and scalar data
119  //- and assign the result.
120  // \param [out] vs vector-space (indexed) data
121  // \param [in] vs1 vector-space (indexed) data operand
122  // \param [in] s scalar operand
123  // \param bop binary operation
124  template<class V, class V1, class S, class BinaryOp>
125  static inline void opVS(V& vs, const V1& vs1, const S& s, BinaryOp bop)
126  {
127  // if constexpr (I < N)
128  {
129  vs.v_[I] = bop(vs1.v_[I], s);
130  VectorSpaceOps<N, I+1>::opVS(vs, vs1, s, bop);
131  }
132  }
133 
134  //- Apply the binary operation between scalar and vector-space data
135  //- and assign the result.
136  // \param [out] vs vector-space (indexed) data
137  // \param [in] s scalar operand
138  // \param [in] vs1 vector-space (indexed) data operand
139  // \param bop binary operation
140  template<class V, class S, class V1, class BinaryOp>
141  static inline void opSV(V& vs, const S& s, const V1& vs1, BinaryOp bop)
142  {
143  // if constexpr (I < N)
144  {
145  vs.v_[I] = bop(s, vs1.v_[I]);
146  VectorSpaceOps<N, I+1>::opSV(vs, s, vs1, bop);
147  }
148  }
149 
150  //- Apply the binary operation between two vector-space data
151  //- and assign the result.
152  // \param [out] vs vector-space (indexed) data
153  // \param [in] vs1 vector-space (indexed) data operand
154  // \param [in] vs2 vector-space (indexed) data operand
155  // \param bop binary operation
156  template<class V, class V1, class BinaryOp>
157  static inline void op(V& vs, const V1& vs1, const V1& vs2, BinaryOp bop)
158  {
159  // if constexpr (I < N)
160  {
161  vs.v_[I] = bop(vs1.v_[I], vs2.v_[I]);
162  VectorSpaceOps<N, I+1>::op(vs, vs1, vs2, bop);
163  }
164  }
165 };
166 
167 
168 //- Loop termination form, when index and loop count <N> are identical
169 // Not really needed with c++17 'if constexpr' except that gcc-7, gcc-8
170 // produce spurious warnings about unused parameters
171 template<direction N>
172 struct VectorSpaceOps<N, N>
173 {
174  template<class Input, class Output>
175  static inline void copy_n(Input, Output) {}
177  template<class Output, class T>
178  static inline void fill_n(Output, const T&) {}
179 
180  template<class V, class S, class EqOp>
181  static inline void eqOpS(V&, const S&, EqOp) {}
182 
183  template<class S, class V, class EqOp>
184  static inline void SeqOp(S&, const V&, EqOp) {}
185 
186  template<class V1, class V2, class EqOp>
187  static inline void eqOp(V1&, const V2&, EqOp) {}
188 
189  template<class V, class V1, class S, class BinaryOp>
190  static inline void opVS(V&, const V1&, const S&, BinaryOp) {}
191 
192  template<class V, class S, class V1, class BinaryOp>
193  static inline void opSV(V&, const S&, const V1&, BinaryOp) {}
195  template<class V, class V1, class BinaryOp>
196  static inline void op(V&, const V1&, const V1&, BinaryOp) {}
197 };
198 
199 
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201 
202 } // End namespace Foam
204 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
205 
206 #endif
207 
208 // ************************************************************************* //
Recursive execution. Terminating at <N>, starting at index <I>
static void eqOpS(V &vs, const S &s, EqOp eo)
Apply the binary assignment operation to each vector-space component.
static void op(V &vs, const V1 &vs1, const V1 &vs2, BinaryOp bop)
Apply the binary operation between two vector-space data and assign the result.
static void copy_n(Input input, Output result)
Somewhat equivalent to std::copy_n() but with templated loops.
static void SeqOp(S &s, const V &vs, EqOp eo)
Apply the inplace binary reduction operation.
static const Identity< scalar > I
Definition: Identity.H:100
#define EqOp(opName, op)
Definition: ops.H:44
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const Vector< label > N(dict.get< Vector< label >>("N"))
static void eqOp(V1 &vs1, const V2 &vs2, EqOp eo)
Apply the inplace binary assignment operation to the components.
static void opSV(V &vs, const S &s, const V1 &vs1, BinaryOp bop)
Apply the binary operation between scalar and vector-space data and assign the result.
static void fill_n(Output result, const T &val)
Somewhat equivalent to std::fill_n() but with templated loops.
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
static void opVS(V &vs, const V1 &vs1, const S &s, BinaryOp bop)
Apply the binary operation between vector-space and scalar data and assign the result.